<template>
  <el-cascader
    :options="options"
    v-model="selectedOptions"
    @change="handleChange"
    :disabled="disabled"
    size="small">
  </el-cascader>
</template>

<script>
  import {list2tree} from '@/utils/treeUtils';
  import {queryOrgTreeList,getTreeParentId} from '@/api/organization';
  import httpClient from '@/utils/httpClient';

  export default {
    data() {
      return {
        options: [],
        selectedOptions: [],
        list: []
      };
    },

    props: {
      addrParam: {
        default: ""
      },
      disabled:{
        default:false
      }
    },
    watch: {
      'addrParam': function (n, o) {
        this.recursive(n)
      }
    },

    mounted(){
      this.$nextTick(function () {
        this.init()
      })
    },

    methods: {
      handleChange(value) {
        this.$emit('addrChange', this.selectedOptions);
      },
      init(){
        let _this = this;
        let param ={
          "position":"organization_queryOrgTreeList",
          "paramMap":{
          }
        };
        httpClient(queryOrgTreeList,param,function(error, result) {
          if(error){
            _this.$message.error('异常');
            return;
          }
          console.log('result:',result);
          _this.orgTotal = result.length;
          result.forEach(item =>{
            item.label = item.name;
            item.value = item.id;
          })
          _this.options = list2tree(result);
        });
      },

      recursive(id){
        let _this=this;
        let data={
          "json":id
        }
        if(!id){
          _this.selectedOptions = [];
          return;
        }
        httpClient(getTreeParentId,data,function (error, results) {
          /*debugger;*/
          console.log('result:',results);
          let arr = [];
          /*if(error != null){
            _this.$message.error('查询结构树上级组织异常');
            return;
          }*/
          if(results){
            arr = results.message.split(",")
            for(var i = 0; i< arr.length; i++){
              _this.selectedOptions.push(arr[i]);
            }
          }
        })
      }
    },
  };

</script>

<style>
  .el-cascader--small .el-cascader__label {
    line-height: 36px;
  }
</style>
